let login_type = 0
$(function () {
    if ($('.JsQrCode').css('display') == 'none') {
        login_type = 1
    } else {
        login_type = 2
    }
    $('.jsOpenLoginLayer,.op-vip').on('click', '', function () {
        openLoginBox()
    })
})

function openLoginBox() {
    let back_url = window.location.href
    layer.open({
        type: 1,
        skin: 'layui-layer-rim',
        title: '',
        fixed: true,
        shadeClose: false,
        scrollbar: false,
        closeBtn: '',
        shade: 0.8,
        area: ['644px', '570px'],
        content: $('.JsLoginLayer').html(),
        success: function (e) {
            login_type = 2
            if ($('.JsQrCode').css('display') == 'block') {
                login_type = 1
                if (u_token) return
                fetch('/ajax/wechat_login/', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        'back_url': back_url
                    })
                }).then(e => e.json()).then((res) => {
                    let code = res.code || 400
                    if (code != '200') {
                        layerFailed(res.msg)
                        return
                    }
                    let _url = res.data.url
                    $('.JsQrCode').find('iframe').attr('src', _url)
                })
            }
        }
    })
}

$(document).on('click', '.JsWeChatBtn', function () {
    login_type = 1
})
$(document).on('click', '.JsCodeBtn', function () {
    login_type = 2
})
$(document).on('click', '.JsGetVerifyCode', function () {
    if ($('.JsGetVerifyCode').hasClass(`disabled-btn`)) {
        return
    }
    let mobile = $(this).parents('div').find('#phone_num').val()
    let captcha = $(this).parents('div').find('#captcha').val()
    if (mobile.length === 0 || !mobile) {
        layerFailed('请输入手机号')
        return
    } else if (!checkMobile(mobile)) {
        layerFailed('手机号码格式错误')
        return
    }
    if (captcha.length < 4 || !captcha) {
        layerFailed('请输入4位图形验证码')
        return
    }
    fetch('/ajax/send_sms/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            'mobile': mobile, 'captcha': captcha
        })
    }).then(e => e.json()).then((res) => {
        let code = res.code || 400
        if (code != '200') {
            $(this).parents('div').find('#captcha').val('')
            $(this).parents('div').find('#captcha-code').click()
            layerFailed(res.msg)
            return
        }
        layerSuccess(res.msg)
        let time = 60
        $('.JsGetVerifyCode').text(`${time}S后获取`)
        $('.JsGetVerifyCode').addClass(`disabled-btn`)
        let interval = setInterval(() => {
            time -= 1
            $('.JsGetVerifyCode').text(`${time < 10 ? '0' : ''}${time}S后获取`)
            if (time < 0) {
                clearInterval(interval)
                $('.JsGetVerifyCode').text(`获取验证码`)
                $('.JsGetVerifyCode').removeClass(`disabled-btn`)
            }
        }, 1000)
    })
})
$(document).on('click', '.JsLoginBtn', function (e) {
    console.log(`login_type:${login_type}`)
    if (login_type != '2') return false
    let phone_num = $(this).parents('div').find('#phone_num').val()
    let verity_code = $(this).parents('div').find('#verity_code').val()
    if (phone_num.length === 0 || !phone_num) {
        layerFailed('请输入手机号')
        return
    } else if (!checkMobile(phone_num)) {
        layerFailed('手机号码格式错误')
        return
    }
    if (verity_code.length != '6') {
        layerFailed('请输入正确的短信验证码')
        return
    }
    fetch('/ajax/phoneLogin/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            'phone_num': phone_num, 'verity_code': verity_code
        })
    }).then(e => e.json()).then((res) => {
        let code = res.code || 400
        if (code != '200') {
            layerFailed(res.msg)
            return
        }
        layerSuccess(res.msg)
        setTimeout(function () {
            location.reload()
        }, 800)
    })
})

function hanleLoginType(type) {
    if (type === 0) {
        $('.qr-code').show()
        $('.tab-title .i-weixing-icon').show()
        $('.tab-title .i-login-phone').hide()
        $('.tab-title-text span').text('微信扫码登录')
        $('.phonelogin').hide()
    } else {
        $('.qr-code').hide()
        $('.tab-title .i-weixing-icon').hide()
        $('.tab-title .i-login-phone').show()
        $('.tab-title-text span').text('手机验证码登录')
        $('.phonelogin').show()
    }
}

function closeLayer() {
    layer.closeAll()
}

function changeImgCode(obj) {
    let $obj = $(obj)
    let _time = (new Date()).getTime();
    $obj.attr('src', '/outside/captcha/?_t=' + _time);
}